package ynu.edu.achitecturework.common.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import ynu.edu.achitecturework.common.dao.LogicDAO;
import ynu.edu.achitecturework.common.entity.LogicEntity;
import ynu.edu.achitecturework.common.result.Result;
import ynu.edu.achitecturework.common.service.LogicService;

import java.io.Serializable;
import java.util.List;

public abstract class LogicController<S extends LogicService<D,T,ID>, D extends LogicDAO<T,ID>,T extends LogicEntity, ID extends Serializable> {
    protected S service;

    public LogicController(S service) {
        this.service = service;
    }

    @GetMapping("/get")
    @ApiOperation("通过ID获取实体")
    public Result<T> GET(@RequestParam ID id) {
        return Result.success(service.GET(id));
    }

    @GetMapping("/getall")
    @ApiOperation("获取所有实体")
    public Result<List<T>> getAll() {
        return Result.success(service.getAll());
    }

    @GetMapping("/getallPage")
    @ApiOperation("分页获取所有实体")
    public Result<Page<T>> getAll(@RequestParam int page, @RequestParam int size) {
        return Result.success(service.getAll(page, size));
    }

    @PostMapping("/put")
    @ApiOperation("修改实体")
    public Result<T> PUT(@RequestBody T entity) {
        return Result.success(service.PUT(entity));
    }

    @PostMapping("/post")
    @ApiOperation("创建实体")
    public Result<T> POST(@RequestBody T entity) {
        return Result.success(service.POST(entity));
    }

    @PostMapping("/delete")
    @ApiOperation("删除实体")
    public Result<Void> DELETE(@RequestBody T entity) {
        service.DELETE(entity);
        return Result.success(null);
    }

    @GetMapping("/delete")
    @ApiOperation("通过ID删除实体")
    public Result<Void> DELETE(@RequestParam ID id) {
        service.DELETE(id);
        return Result.success(null);
    }
}